// $Id: headers.c 46 2011-04-16 08:39:45Z cedric.shih@gmail.com $
/*
 * Copyright (C) 2010 Cedric Shih <cedric.shih@gmail.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/tree.h>

#include "log.h"

#include "headers.h"

struct niohttp_headers *niohttp_headers_new(void)
{
	struct niohttp_headers *headers;

	niohttp_debug("constructing");

	if ((headers = calloc(1, sizeof(struct niohttp_headers))) == NULL) {
		niohttp_error("failed to calloc");
		return NULL;
	}

	TAILQ_INIT(&headers->list);
	RB_INIT(&headers->tree);

	return headers;
}

void niohttp_headers_free(struct niohttp_headers *headers)
{

	if (headers == NULL) return;

	niohttp_debug("destructing");

	niohttp_headers_clear(headers);
	free(headers);
}

struct niohttp_headers *niohttp_headers_clone(
		struct niohttp_headers *source, int strdup)
{
	int rc;
	struct niohttp_headers *headers;

	if ((headers = niohttp_headers_new()) &&
			(rc = niohttp_headers_load(headers, source))) {
		niohttp_error("failed to load headers: %s", strerror(rc));
		niohttp_headers_free(headers);
		headers = NULL;
	}

	return headers;
}

int niohttp_headers_load(struct niohttp_headers *headers,
		struct niohttp_headers *source)
{
	int rc;
	struct niohttp_header *header;

	TAILQ_FOREACH(header, &source->list, next) {
		if ((rc = niohttp_headers_set(headers, header->name,
				header->value))) {
			niohttp_error("failed to set header '%s': %s",
					header->name, strerror(rc));
			goto finally;
		}
	}

	rc = 0;

finally:
	return rc;
}

inline struct niohttp_headers_entry *niohttp_headers_find(
		struct niohttp_headers *headers, const char *name)
{
	struct niohttp_headers_entry find;

	find.name = (char *)name;
	return RB_FIND(niohttp_headers_tree, &headers->tree, &find);
}

inline const char *niohttp_headers_get(struct niohttp_headers *headers,
		const char *name)
{
	struct niohttp_headers_entry *entry;

	if ((entry = niohttp_headers_find(headers, name)) != NULL) {
		return entry->value;
	} else {
		return NULL;
	}
}

inline int niohttp_headers_set(struct niohttp_headers *headers,
		const char *name, const char *value)
{
	return niohttp_headers_nset(headers, name, strlen(name),
			value, strlen(value));
}

int niohttp_headers_nset(struct niohttp_headers *headers,
		const char *name, size_t namelen,
		const char *value, size_t valuelen)
{
	int rc;
	struct niohttp_headers_entry *entry;
	char *dup;

	if (!headers || !name || !value) {
		niohttp_error("null argument(s)");
		rc = NIOHTTP_EINVAL;
		goto finally;
	}

	niohttp_debug("setting '%.*s' = '%.*s'", namelen, name,
			valuelen, value);

	if (!(entry = niohttp_headers_find(headers, name))) {
		if (!(entry = calloc(1, sizeof(struct niohttp_headers_entry)))) {
			rc = NIOHTTP_ENOMEM;
			niohttp_error("failed to calloc");
			goto finally;
		}

		if (!(entry->name = malloc(namelen + 1))) {
			rc = NIOHTTP_ENOMEM;
			niohttp_error("failed to malloc");
			free(entry);
			goto finally;
		}

		memcpy(entry->name, name, namelen);
		entry->name[namelen] = '\0';

		if (!(entry->value = malloc(valuelen + 1))) {
			rc = NIOHTTP_ENOMEM;
			niohttp_error("failed to malloc");
			free(entry->name);
			free(entry);
			goto finally;
		}

		memcpy(entry->value, value, valuelen);
		entry->value[valuelen] = '\0';

		RB_INSERT(niohttp_headers_tree, &headers->tree, entry);
		entry->node.name = entry->name;
		entry->node.value = entry->value;
		TAILQ_INSERT_TAIL(&headers->list, &entry->node, next);

		headers->size++;
	} else {
		if (strcmp(entry->value, value)) {
			if (!(dup = malloc(valuelen + 1))) {
				rc = NIOHTTP_ENOMEM;
				niohttp_error("failed to malloc");
				goto finally;
			}

			memcpy(dup, value, valuelen);
			dup[valuelen] = '\0';

			free(entry->value);
			entry->value = dup;
		}
	}

	rc = 0;

finally:
	return rc;
}

int niohttp_headers_remove(struct niohttp_headers *headers, const char *name)
{
	struct niohttp_headers_entry *entry;

	niohttp_debug("removing: %s", name);

	if ((entry = niohttp_headers_find(headers, name)) != NULL) {
		niohttp_headers_remove_do(headers, entry);
		return 0;
	} else {
		niohttp_error("not found: %s", name);
		return NIOHTTP_ENOENT;
	}
}

inline void niohttp_headers_remove_do(struct niohttp_headers *headers,
		struct niohttp_headers_entry *entry)
{
	TAILQ_REMOVE(&headers->list, &entry->node, next);
	RB_REMOVE(niohttp_headers_tree, &headers->tree, entry);

	free(entry->name);
	free(entry->value);
	free(entry);

	headers->size--;
}

inline struct niohttp_header_list *niohttp_headers_list(
		struct niohttp_headers *headers)
{
	if (!headers) {
		return NULL;
	} else  {
		return &headers->list;
	}
}

void niohttp_headers_clear(struct niohttp_headers *headers)
{
	struct niohttp_headers_entry *entry;

	niohttp_debug("clearing");

	while ((entry = RB_ROOT(&headers->tree))) {
		niohttp_headers_remove_do(headers, entry);
	}
}

inline size_t niohttp_headers_size(struct niohttp_headers *headers)
{
	return headers->size;
}

inline int niohttp_headers_entry_compare(struct niohttp_headers_entry *a,
		struct niohttp_headers_entry *b)
{
	return (strcasecmp(a->name, b->name));
}

RB_GENERATE(niohttp_headers_tree, niohttp_headers_entry, next,
		niohttp_headers_entry_compare);
